package com.study.demo.ui.report;

import android.support.annotation.NonNull;

import com.stduy.injector.PerActivity;
import com.study.demo.api.forum.ForumApi;
import com.study.demo.bean.BaseData;
import com.study.demo.util.ToastUtil;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;


/**
 * Created by sll on 2016/3/11.
 */
@PerActivity
public class ReportPresenter implements ReportContract.Presenter {

    private ForumApi mForumApi;

    @Inject
    public ReportPresenter(ForumApi forumApi) {
        mForumApi = forumApi;
    }

    private Disposable disposable;
    private ReportContract.View mReportView;

    public void submitReports(String tid, String pid, String type, String content) {
        mReportView.showLoading();
        disposable = mForumApi.submitReports(tid, pid, type, content)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this::onRequestSuccess, this::onRequestFailed);
    }

    private void onRequestSuccess(BaseData result) {
        mReportView.hideLoading();
        if (result.status == 200) {
            mReportView.reportSuccess();
            ToastUtil.showToast("举报成功~");
        } else {
            ToastUtil.showToast("举报失败，请检查网络后重试");
        }
    }

    private void onRequestFailed(Throwable throwable) {
        mReportView.hideLoading();
        ToastUtil.showToast("举报失败，请检查网络后重试");
    }

    @Override
    public void attachView(@NonNull ReportContract.View view) {
        mReportView = view;
    }

    @Override
    public void detachView() {
        if (disposable != null && !disposable.isDisposed()) {
            disposable.dispose();
        }
        mReportView = null;
    }
}
